<template>
  <div>
    <!-- table -->
    <a-card :bordered="false">
      <a-row>
        <a-col :sm="8" :xs="8">
          <head-info title="$t('rechargeList.ddzf')" :content="stat[0]" :bordered="true"/>
        </a-col>
        <a-col :sm="8" :xs="8">
          <head-info title="$t('rechargeList.zfcg')" :content="stat[1]" :bordered="true"/>
        </a-col>
        <a-col :sm="8" :xs="8">
          <head-info title="$t('rechargeList.czze')" :content="stat[2]"/>
        </a-col>
      </a-row>
    </a-card>
    <a-card style="margin-top: 24px">
      <div class="table-operator">
        <div style="margin-bottom:15px;">
          {{ $t('rechargeList.lxsx') }}:
          <a-radio-group @change="handleChangeType" defaultValue="-1">
            <a-radio-button value="-1">{{ $t('rechargeList.qb') }}</a-radio-button>
            <a-radio-button value="buyer">{{ $t('rechargeList.mj') }}</a-radio-button>
            <a-radio-button value="shop">{{ $t('rechargeList.sj') }}</a-radio-button>
          </a-radio-group>
          <a-range-picker style="margin:0 10px;" @change="handleClickChangeDate" :style="{marginLeft:'10px',width: '256px'}" />
          <a-input-search style="margin-left:10px;width:280px;" placeholder="$t('rechargeList.sssjh')" @search="onSearch" enterButton />
        </div>
      </div>
      <a-table
        :columns="columns"
        :dataSource="data"
        :pagination="false"
        :loading="dataLoading"
      >

        <template slot="status" slot-scope="text">
          <template v-if="text==0">
            <a-tag color="grey">{{ $t('rechargeList.xd') }}</a-tag>
          </template>
          <template v-else-if="text==1">
            <a-tag color="green">{{ $t('rechargeList.cg') }}</a-tag>
          </template>
          <template v-if="text==-1">
            <a-tag color="red">{{ $t('rechargeList.sb') }}</a-tag>
          </template>
        </template>
<!--        <template slot="text" slot-scope="text, record">-->
<!--          <template>-->
<!--            <span>{{text}}</span>-->
<!--          </template>-->
<!--        </template>-->
<!--        <template slot="operation" slot-scope="text, record">-->
<!--          <span>-->
<!--            <a-popconfirm title="$t('rechargeList.sfysccx')" @confirm="handleClickDelete(record.id)">-->
<!--              <a-button type="danger" size="small">{{ $t('rechargeList.sc') }}</a-button>-->
<!--            </a-popconfirm>-->
<!--          </span>-->
<!--        </template>-->
      </a-table>
      <div style="margin-top:15px;">
        <MyPage :curr="page.curr" :size="page.size" :all="page.total" @changePage="handleChangePage" @changePageSize="handleChangePageSize"/>
      </div>
    </a-card>
  </div>
</template>

<script>
import HeadInfo from '@/components/tools/HeadInfo'
import { mixin, mixinDevice } from '@/utils/mixin'
import MyPage from './Page'

export default {
  name: 'AdvancedForm',
  mixins: [mixin, mixinDevice],
  components: {
    HeadInfo,
    MyPage
  },
  data () {
    return {
      description: this.$t('rechargeList.bhczdlsjl'),
      loading: false,
      dataLoading: false,
      stat: [
        0, 0, 0
      ],
      // table
      columns: [
        {
          title: this.$t('rechargeList.ddh'),
          dataIndex: 'order_id',
          key: 'order_id',
          scopedSlots: { customRender: 'order_id' }
        },
        {
          title: this.$t('rechargeList.lx'),
          dataIndex: 'belong',
          key: 'belong',
          scopedSlots: { customRender: 'belong' }
        },
        {
          title: this.$t('rechargeList.zh'),
          dataIndex: 'username',
          key: 'username',
          scopedSlots: { customRender: 'username' }
        },
        {
          title: this.$t('rechargeList.jq'),
          dataIndex: 'money',
          key: 'money'
        },
        {
          title: this.$t('rechargeList.czfs'),
          dataIndex: 'pay_type',
          key: 'pay_type'
        },
        {
          title: this.$t('rechargeList.dqzs'),
          dataIndex: 'status',
          key: 'status',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: this.$t('rechargeList.sj'),
          dataIndex: 'created_at',
          key: 'created_at',
          scopedSlots: { customRender: 'created_at' }
        }
        // {
        //   title: this.$t('rechargeList.cz'),
        //   key: 'action',
        //   scopedSlots: { customRender: 'operation' }
        // }
      ],
      data: [],
      errors: [],
      readyCnt: 0,
      runningCnt: 0,
      stopCnt: 0,
      page: {
        curr: 1,
        size: 20,
        total: 0
      },
      searchUsername: '',
      date_str: ''
    }
  },
  created () {
    this.getList()
  },
  methods: {
    getList (type) {
      let url = '/recharge/list?page=' + this.page.curr + '&size=' + this.page.size
      if (this.searchUsername) {
        url += '&username=' + this.searchUsername
      }
      if (type !== undefined && type !== '-1') {
        url += '&type=' + type
      }
      if (this.date_str) {
        url += '&date_str=' + this.date_str
      }
      this.dataLoading = true
      this.$http.get(url).then(res => {
        this.data = res.list
        this.page.total = res.total
        let status1 = 0
        let status2 = 0

        this.data.forEach(item => {
          switch (item.status) {
            case 0:
              status1++
              break
            case 1:
              status2++
              break
          }
        })
        this.stat = [status1, status2, res.sum]
      }).catch(err => {
        this.$notification.error({
          message: this.$t('rechargeList.sb'),
          description: err.message
        })
      }).then(() => {
        this.dataLoading = false
      })
    },
    handleClickChangeDate (e, str) {
      this.date_str = str
      this.getList()
    },
    onSearch (val) {
      if (this.dataLoading) {
        return
      }
      this.searchUsername = val
      this.getList()
    },
    handleClickDelete (id) {
      this.$http.post('/recharge/delete', this.$qs.stringify({
        id: id
      })).then(res => {
        const newData = this.data.filter(item => item.id !== id)
        this.data = newData
      }).catch(err => {
        this.$notification.error({
          message: this.$t('rechargeList.sb'),
          description: err.message
        })
      })
    },
    handleSubmit (e) {
      e.preventDefault()
    },
    handleChangeType (e) {
      this.getList(e.target.value)
    },
    handleChangePageSize (size) {
      this.page.size = size
      this.getList()
    },
    handleChangePage (page) {
      this.page.curr = page
      this.getList()
    }
  }
}
</script>

<style lang="less" scoped>
  .card{
    margin-bottom: 24px;
  }
</style>
